package swing.project.standard;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ResourceBundle;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Logger;

import com.h819.core.commons.Configurations;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

/**
 * @Title: Jpanel2.java
 * @Description: TODO(添加描述)
 * @author H819
 * @date 2010-10-10
 * @version V1.0
 */

public class Jpanel10 extends JPanel implements ActionListener {
	private JLabel statuslable;
	private static Logger log = Logger.getLogger(Jpanel10.class);
	private JLabel infolabel = new JLabel("");

	private static final ResourceBundle BUNDLE = ResourceBundle
			.getBundle("swing.project.standard.messages"); //$NON-NLS-1$

	private PropertiesConfiguration conf = Configurations.getInstance(new File(BUNDLE
			.getString("app.config.filepath")));
	
	private String selectFilePath ="";

	private final JFileChooser fileChooser = new JFileChooser();

	private File selectFile = null;

	private final JCheckBox checkBox = new JCheckBox("");
	private final JButton btnExcel = new JButton(
			BUNDLE.getString("Jpanel10.btnExcel.text")); //$NON-NLS-1$ //$NON-NLS-2$
	private final JLabel lblExcel = new JLabel(
			BUNDLE.getString("Jpanel10.lblExcel.text")); //$NON-NLS-2$ //$NON-NLS-1$
	private final JLabel lblPdf = new JLabel(
			BUNDLE.getString("Jpanel10.lblPdf.text")); //$NON-NLS-1$ //$NON-NLS-2$
	private final JButton button = new JButton(
			BUNDLE.getString("Jpanel10.btnExcel.text")); //$NON-NLS-1$ //$NON-NLS-2$
	private final JLabel label = new JLabel(
			BUNDLE.getString("Jpanel10.label.text")); //$NON-NLS-1$ //$NON-NLS-2$
	private final JLabel label_1 = new JLabel(
			BUNDLE.getString("Jpanel10.label_1.text")); //$NON-NLS-1$ //$NON-NLS-2$
	private final JButton button_1 = new JButton(
			BUNDLE.getString("Jpanel10.button_1.text")); //$NON-NLS-1$ //$NON-NLS-2$

	/**
	 * Create the panel.
	 */
	public Jpanel10() {

		setLayout(new FormLayout(
				new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.GROWING_BUTTON_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.PREF_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.PREF_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.PREF_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.GLUE_COLSPEC, },
				new RowSpec[] { FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.PARAGRAPH_GAP_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.PREF_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.PREF_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.PREF_ROWSPEC, RowSpec.decode("-11px:grow"), }));

		JLabel maininfo = new JLabel(BUNDLE.getString("Jpanel10.maininfo.text")); //$NON-NLS-1$ //$NON-NLS-2$
		add(maininfo, "2, 4, 21, 1");

		add(label, "2, 7");
		checkBox.setSelected(true);

		add(checkBox, "12, 7");

		add(lblExcel, "2, 9");

		ButtonGroup group = new ButtonGroup();

		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

		btnExcel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// 加载原设置
				fileChooser.setCurrentDirectory(new File(conf
						.getString("config.excel.tilucheck.root")));

				// 获得传入的文件
				int returnValue = fileChooser.showOpenDialog(null);
				if (returnValue == JFileChooser.APPROVE_OPTION) {
					
					selectFile = fileChooser.getSelectedFile();
					
					selectFilePath = selectFile.getAbsolutePath();

					// 此处弹出对话框
					JOptionPane.showMessageDialog(Jpanel10.this, "确认要使用 "
							+ selectFile.getAbsolutePath() + " 作为题录验证的默认目录 ？");

					//

					conf.setProperty("config.excel.tilucheck.root",
							selectFilePath);

					// ls.add("excel.tilucheck.root=");
					// ls.add("pdf.download.root=");

					log.info(selectFile.getPath());
				} else {

					return;
				}

			}
		});

		add(btnExcel, "12, 9");

		add(lblPdf, "2, 11");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				// 加载原设置
				fileChooser.setCurrentDirectory(new File(conf
						.getString("config.pdf.download.root")));

				// 获得传入的文件
				int returnValue = fileChooser.showOpenDialog(null);
				if (returnValue == JFileChooser.APPROVE_OPTION) {
					selectFile = fileChooser.getSelectedFile();
					
					selectFilePath = selectFile.getAbsolutePath();

					// 此处弹出对话框
					JOptionPane.showMessageDialog(Jpanel10.this, "确认要使用 "
							+ selectFile.getAbsolutePath()
							+ " 作为PDF文件下载的默认目录 ？");

					//

					conf.setProperty("config.pdf.download.root",
							selectFilePath);

					// ls.add("excel.tilucheck.root=");
					// ls.add("pdf.download.root=");

					log.info(selectFile.getPath());
				} else {

					return;
				}
			}
		});

		add(button, "12, 11");

		add(label_1, "2, 13");

		add(button_1, "12, 13");
	}

	/**
	 * Listens to the radio group.add(radioButton_2); buttons.
	 */
	public void actionPerformed(ActionEvent e) {

		AbstractButton aButton = (AbstractButton) e.getSource();
		System.out.println(aButton.getText());

		// System.out.println("radioButton is selsected :"+radioButton.isSelected());
		// System.out.println("radioButton_1 is selsected :"+radioButton_1.isSelected());
		// System.out.println("radioButton_2 is selsected :"+radioButton_2.isSelected());
		//

		if (aButton.getText().equals("默认")) {
			checkBox.setSelected(true);

		} else if (aButton.getText().equals("全选")) {
			checkBox.setSelected(true);

		} else if (aButton.getText().equals("全不选")) {
			checkBox.setSelected(false);

		} else {

		}

	}
}